//
//  TTPresentAnimation.m
//  TaoTuo
//
//  Created by admin on 2019/8/28.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLPresentAnimation.h"

@implementation LLPresentAnimation

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.3;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    self.isPresent ? [self presentAnimationTranstion:transitionContext] : [self dismissAnimationTranstion:transitionContext];
}

// 推出
- (void)presentAnimationTranstion:(id<UIViewControllerContextTransitioning>)transitionContext
{
    // 控制器模态的时候，加了这个 oneVC.modalPresentationStyle = UIModalPresentationCustom; 就不用再把fromVC的view加上来了，因为fromVC会一直存在，同理模态消失的时候也不用再把toVC的viewj添加到 containerView 中了
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    CGSize size = [UIScreen mainScreen].bounds.size;
    toVC.view.frame = CGRectMake(0, size.height, size.width, size.height * 0.85);
    
    // 添加控制器的view
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];
    containerView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    
//    // 添加一个view，点击使 toVC dismiss
    UIView *tapView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, toVC.view.width, size.height * 0.15)];
    [containerView addSubview:tapView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(UITapGestureRecognizer *  _Nonnull sender) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:detailFilterViewControllerDismiss object:nil];
        
    }];
    [tapView addGestureRecognizer:tap];
    
    // 做动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        toVC.view.frame = CGRectMake(0, size.height * 0.15, size.width, size.height * 0.85);
        containerView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
    
}

// 消失
- (void)dismissAnimationTranstion:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    //     添加控制器的view
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:fromVC.view];
    
    
    CGSize size = [UIScreen mainScreen].bounds.size;
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        fromVC.view.frame = CGRectMake(0, size.height, size.width, size.height * 0.85);
        containerView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    } completion:^(BOOL finished) {
        
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
    
}



@end
